using System;
using System.Data;
using PCSComProcurement.Purchase.DS;
using PCSComUtils.MasterSetup.DS;
using PCSComUtils.PCSExc;
using PCSComUtils.Common;

namespace PCSComProcurement.Purchase.BO
{
	public class POFunctionVendorCrossReferenceBO
	{
		private const string THIS = "PCSComProcurement.Purchase.BO.POFunctionVendorCrossReferenceBO";
		public POFunctionVendorCrossReferenceBO()
		{
			//
			// TODO: Add constructor logic here
			//
		}
		
		//[AutoComplete(true)]
		public void Add(object pObjectDetail)
		{
			// TODO:  
			try
			{
				new PO_FunctionVendorReferenceDS().Add(pObjectDetail);
			}
			catch (PCSDBException ex)
			{
				throw ex;
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
	
		
		public void Delete(object pObjectVO)
		{
		}
		
		//[AutoComplete(true)]
		public void Delete(int pintID)
		{	
			try
			{
				new PO_FunctionVendorReferenceDS().Delete(pintID);
			}
			catch (PCSDBException ex)
			{
				throw ex;
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}


	
		public object GetObjectVO(int pintID, string VOclass)
		{
			// TODO:  
			return null;
		}

		//[AutoComplete(true)]
		public object GetObjectVO(int pintVendorID,int pintFunctionID)
		{
			// TODO:  
			try
			{
				return new PO_FunctionVendorReferenceDS().GetObjectVO(pintVendorID, pintFunctionID);
			}
			catch (PCSDBException ex)
			{
				throw ex;
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}


		//**************************************************************************              
		///    <Description>
		///		Get all functionvendorcross by CCN, Vendor & Location
		///    </Description>
		///    <Inputs>
		///    </Inputs>
		///    <Outputs>
		///    </Outputs>
		///    <Returns>
		///       DataTable
		///    </Returns>
		///    <Authors>
		///       TuanDm
		///    </Authors>
		///    <History>
		///       Monday, Apr 04, 2005
		///    </History>
		///    <Notes>
		///    </Notes>
		//**************************************************************************
		//[AutoComplete(true)]
		public DataTable ListFunctionVendorCrossReference(int pintCCNID,int pintVendorID,int pintVendorLocID)
		{
			try
			{
				return new PO_FunctionVendorReferenceDS().ListFunctionVendorCrossReference(pintCCNID,pintVendorID,pintVendorLocID).Tables[0];	
			}
			catch (PCSDBException ex)
			{
				throw ex;
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
		
		//[AutoComplete(true)]
		public void Update(object pObjectDetail)
		{
			// TODO:  
			// TODO:  
			try
			{
				new PO_FunctionVendorReferenceDS().Update(pObjectDetail);
			}
			catch (PCSDBException ex)
			{
				throw ex;
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
	
		public void UpdateDataSet(DataSet dstData)
		{
			// TODO:  

		}


		//**************************************************************************              
		///    <Description>
		///    Get the list of party location by party id
		///    </Description>
		///    <Inputs>
		///    CCNID
		///    </Inputs>
		///    <Outputs>
		///    </Outputs>
		///    <Returns>
		///       DataTable
		///    </Returns>
		///    <Authors>
		///       TuanDm
		///    </Authors>
		///    <History>
		///       Monday, Apr 04, 2005
		///    </History>
		///    <Notes>
		///    </Notes>
		//**************************************************************************
		//[AutoComplete(true)]
		public DataTable ListPartyLocByPartyID(int pintPartyID)
		{
			try
			{
				MST_PartyLocationDS dsPartyLoc = new MST_PartyLocationDS();
				return dsPartyLoc.ListPartyLocation(pintPartyID).Tables[0];
			}
			catch (PCSException ex)
			{
				throw ex.CauseException;
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}


		//**************************************************************************              
		///    <Description>
		///    List all carrier
		///    </Description>
		///    <Inputs>
		///    </Inputs>
		///    <Outputs>
		///    </Outputs>
		///    <Returns>
		///       void
		///    </Returns>
		///    <Authors>
		///       TuanDm
		///    </Authors>
		///    <History>
		///       Tuesday, Mar 21, 2005
		///    </History>
		///    <Notes>
		///    </Notes>
		//**************************************************************************
		//[AutoComplete(true)]
		public DataTable ListCarrier()
		{
			try
			{
				MST_CarrierDS dsCarrier = new MST_CarrierDS();
				return dsCarrier.List().Tables[0];
			}
			catch (PCSDBException ex)
			{
				throw ex;
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}


		//**************************************************************************              
		///    <Description>
		///    Get the list of currency
		///    </Description>
		///    <Inputs>
		///    </Inputs>
		///    <Outputs>
		///    </Outputs>
		///    <Returns>
		///       DataTable
		///    </Returns>
		///    <Authors>
		///       TuanDm
		///    </Authors>
		///    <History>
		///       Wednesday, Apr 06, 2005
		///    </History>
		///    <Notes>
		///    </Notes>
		//**************************************************************************
		//[AutoComplete(true)]
		public DataTable ListCurrency()
		{
			try
			{
				MST_CurrencyDS dsCurrency = new MST_CurrencyDS();
				return dsCurrency.List().Tables[0];
			}
			catch (PCSDBException ex)
			{
				throw ex;
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}

		//**************************************************************************              
		///    <Description>
		///    Get the list of function
		///    </Description>
		///    <Inputs>
		///    </Inputs>
		///    <Outputs>
		///    </Outputs>
		///    <Returns>
		///       DataTable
		///    </Returns>
		///    <Authors>
		///       TuanDm
		///    </Authors>
		///    <History>
		///       Wednesday, Apr 06, 2005
		///    </History>
		///    <Notes>
		///    </Notes>
		//**************************************************************************
		//[AutoComplete(true)]
		public DataTable ListFunction()
		{
			try
			{
				MST_FunctionDS dsFunction = new MST_FunctionDS();
				return dsFunction.List().Tables[0];
			}
			catch (PCSDBException ex)
			{
				throw ex;
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}


		//**************************************************************************              
		///    <Description>
		///		Create the query string to search the vendor 
		///    </Description>
		///    <Inputs>
		///     Value, and Type
		///    </Inputs>
		///    <Outputs>
		///    Type 0 : Find by Code and Edit Mode
		///    Type 1 : Find by Name and Edit Mode
		///    Type 2 : Find by Code and Add Mode
		///    Type 3 : Find by Name and Add Mode
		///    </Outputs>
		///    <Returns>
		///       string
		///    </Returns>
		///    <Authors>
		///       TuanDm
		///    </Authors>
		///    <History>
		///       Monday, Apr 04, 2005
		///    </History>
		///    <Notes>
		///    </Notes>
		//**************************************************************************
		//[AutoComplete(true)]
		public string ConditionToSearchVendor(string pstrValue, int type)
		{
			string strConditon = string.Empty;
			switch (type)
			{
				case 0:
					strConditon = "Where " + MST_PartyTable.PARTYID_FLD + " in " + "(Select " + PO_FunctionVendorReferenceTable.VENDORID_FLD + " From " + PO_FunctionVendorReferenceTable.TABLE_NAME + ")";
				break;

				case 1:
					strConditon = "Where " + MST_PartyTable.PARTYID_FLD + " in " + "(Select " + PO_FunctionVendorReferenceTable.VENDORID_FLD + " From " + PO_FunctionVendorReferenceTable.TABLE_NAME + ")";
				break;

				case 2:
					strConditon = "Where " + MST_PartyTable.PARTYID_FLD + " not in " + "(Select " + PO_FunctionVendorReferenceTable.VENDORID_FLD + " From " + PO_FunctionVendorReferenceTable.TABLE_NAME + ")";
				break;

				case 3:
					strConditon = "Where " +  MST_PartyTable.PARTYID_FLD + " not in " + "(Select " + PO_FunctionVendorReferenceTable.VENDORID_FLD + " From " + PO_FunctionVendorReferenceTable.TABLE_NAME + ")";
				break;
			}
			
			strConditon += " And " + MST_PartyTable.TYPE_FLD + " = 1";
			return strConditon;
		}

		//**************************************************************************              
		///    <Description>
		///		Create the query string to search the function 
		///    </Description>
		///    <Inputs>
		///     Vender Code, Type 0 : find to Edit, 1 : find to Add
		///    </Inputs>
		///    <Outputs>
		///    </Outputs>
		///    <Returns>
		///       string
		///    </Returns>
		///    <Authors>
		///       TuanDm
		///    </Authors>
		///    <History>
		///       Monday, Apr 04, 2005
		///    </History>
		///    <Notes>
		///    </Notes>
		//**************************************************************************
		//[AutoComplete(true)]
		public string ConditionToSearchFunction(string pstrValue, int type)
		{
			string strConditon = string.Empty;
			switch (type)
			{
				case 0:
					strConditon = "Where " + MST_FunctionTable.CODE_FLD +  " like '" + pstrValue.Trim() + "%' "
						+ " and " + MST_FunctionTable.FUNCTIONID_FLD + " in " + "(Select " + PO_FunctionVendorReferenceTable.FUNCTIONID_FLD + " From " + PO_FunctionVendorReferenceTable.TABLE_NAME + ")";
					break;

				case 1:
					strConditon = "Where " + MST_FunctionTable.CODE_FLD +  " like '" + pstrValue.Trim() + "%' "
						+ " and " + MST_FunctionTable.FUNCTIONID_FLD + " not in " + "(Select " + PO_FunctionVendorReferenceTable.FUNCTIONID_FLD + " From " + PO_FunctionVendorReferenceTable.TABLE_NAME + ")";
					break;
			}
			return strConditon;
		}
		
		//**************************************************************************              
		///    <Description>
		///		Create the query string to search the vendor 
		///    </Description>
		///    <Inputs>
		///     
		///     Type 0 : find to Edit by Code
		///     Type 1 : find to Edit by Name
		///     Type 2 : find to Add by Code
		///     Type 3 : find to Add by Name
		///    </Inputs>
		///    <Outputs>
		///    </Outputs>
		///    <Returns>
		///       string
		///    </Returns>
		///    <Authors>
		///       TuanDm
		///    </Authors>
		///    <History>
		///       Monday, Apr 04, 2005
		///    </History>
		///    <Notes>
		///    </Notes>
		//**************************************************************************
		//[AutoComplete(true)]
		public string ConditionToSearchVendorByFunctionCode(string pstrFunctionCode, int type)
		{
			string strConditon = string.Empty;
			switch (type)
			{
				case 0:
					strConditon =	" Where  (Select count(*) From " 
						+  PO_FunctionVendorReferenceTable.TABLE_NAME + " A inner join " + MST_FunctionTable.TABLE_NAME + " B on A." + MST_FunctionTable.FUNCTIONID_FLD + " = B." + MST_FunctionTable.FUNCTIONID_FLD 
						+  " Where B." + MST_FunctionTable.CODE_FLD + " like '" + pstrFunctionCode.Trim() + "%' and " + MST_PartyTable.PARTYID_FLD + " = A." + PO_FunctionVendorReferenceTable.VENDORID_FLD + ") > 0";
					break;
				case 1:
					strConditon =	" Where (Select count(*) From "
						+ PO_FunctionVendorReferenceTable.TABLE_NAME + " A inner join " + MST_FunctionTable.TABLE_NAME + " B on A." + MST_FunctionTable.FUNCTIONID_FLD + " = B." + MST_FunctionTable.FUNCTIONID_FLD
						+ " Where B." + MST_FunctionTable.CODE_FLD + " like '" + pstrFunctionCode.Trim() + "%' and " + MST_PartyTable.PARTYID_FLD + " = A." + PO_FunctionVendorReferenceTable.VENDORID_FLD + ") > 0";
					break;
				case 2:
					strConditon = " Where (Select count(*) From "
						+ PO_FunctionVendorReferenceTable.TABLE_NAME + " A Where A." + PO_FunctionVendorReferenceTable.VENDORID_FLD + " = " + MST_PartyTable.PARTYID_FLD + ") <   (Select count(*) From " 
						+ MST_FunctionTable.TABLE_NAME + " Where " + MST_FunctionTable.CODE_FLD + "  like '" + pstrFunctionCode.Trim() + "%')";
					break;
				case 3:
					strConditon = " Where ( Select count(*) From " 
						+ PO_FunctionVendorReferenceTable.TABLE_NAME + " A Where A." +  PO_FunctionVendorReferenceTable.VENDORID_FLD + " = " + MST_PartyTable.PARTYID_FLD + ") <   (Select count(*) From " 
						+ MST_FunctionTable.TABLE_NAME + " Where " +  MST_FunctionTable.CODE_FLD + " like '" + pstrFunctionCode.Trim() + "%')";
					break;
			}
			return strConditon;
		}
		
		//**************************************************************************              
		///    <Description>
		///		Create the query string to search the function 
		///    </Description>
		///    <Inputs>
		///     Type 0 : find to Edit by VendorCode
		///     Type 1 : find to Edit by VendorName
		///     Type 2 : find to Add by VendorCode
		///     Type 3 : find to Add by VendorName
		///    </Inputs>
		///    <Outputs>
		///    </Outputs>
		///    <Returns>
		///       string
		///    </Returns>
		///    <Authors>
		///       TuanDm
		///    </Authors>
		///    <History>
		///       Monday, Apr 04, 2005
		///    </History>
		///    <Notes>
		///    </Notes>
		//**************************************************************************
		//[AutoComplete(true)]
		public string ConditionToSearchFunctionByVendor(string pstrValueOfVendor, int type)
		{
			string strConditon = string.Empty;
			switch (type)
			{
				case 0:
					strConditon = " Where (Select count(*) From "
						+ PO_FunctionVendorReferenceTable.TABLE_NAME + " A inner join " + MST_PartyTable.TABLE_NAME + " B on A."
						+ PO_FunctionVendorReferenceTable.VENDORID_FLD + " = B." + MST_PartyTable.PARTYID_FLD 
						+ " Where A." + PO_FunctionVendorReferenceTable.FUNCTIONID_FLD + " = " + MST_FunctionTable.TABLE_NAME + "." + MST_FunctionTable.FUNCTIONID_FLD 
						+ " And B." + MST_PartyTable.CODE_FLD + " like '" + pstrValueOfVendor.Trim() + "%') > 0";
					break;
				case 1:
					strConditon = " Where (Select count(*) From "
						+ PO_FunctionVendorReferenceTable.TABLE_NAME + " A inner join " +  MST_PartyTable.TABLE_NAME + " B on A."
						+ PO_FunctionVendorReferenceTable.VENDORID_FLD + " = B." + MST_PartyTable.PARTYID_FLD
						+ " Where A." + PO_FunctionVendorReferenceTable.FUNCTIONID_FLD + " = " + MST_FunctionTable.TABLE_NAME + "." + MST_FunctionTable.FUNCTIONID_FLD
						+ " And B." + MST_PartyTable.CODE_FLD + " like '" + pstrValueOfVendor.Trim() + "%') > 0";
					break;
				case 2:
					strConditon = " Where FunctionID not int (Select FunctionID from PO_FunctionVendorReference inner join MST_Party on PO_FunctionVendorReference.VendorID = MST_Party.PartyID"
						+ " and MST_Party.Code like '%" + pstrValueOfVendor + "')";
					break;
				case 3:
					strConditon = " Where (Select count(*) From "
						+ PO_FunctionVendorReferenceTable.TABLE_NAME + " A Where A." + PO_FunctionVendorReferenceTable.FUNCTIONID_FLD + " = "
						+ MST_FunctionTable.TABLE_NAME + "." + MST_FunctionTable.FUNCTIONID_FLD + ") < (Select count(*) From "
						+ MST_PartyTable.TABLE_NAME + " Where " + MST_PartyTable.NAME_FLD + " like '" + pstrValueOfVendor.Trim() + "%')";
					break;
			}
			return strConditon;
		}


		//**************************************************************************              
		///    <Description>
		///		GetRows from the Table with  
		///    </Description>
		///    <Inputs>
		///    </Inputs>
		///    <Outputs>
		///    </Outputs>
		///    <Returns>
		///       void
		///    </Returns>
		///    <Authors>
		///       TuanDm
		///    </Authors>
		///    <History>
		///       Wednesday, Apr 06, 2005
		///    </History>
		///    <Notes>
		///    </Notes>
		//**************************************************************************
		//[AutoComplete(true)]
		public DataTable GetRows(string pstrTableName, string pstrCondition)
		{
			try
			{
				return new PO_FunctionVendorReferenceDS().GetRows(pstrTableName, pstrCondition);	
			}
			catch (PCSDBException ex)
			{
				throw ex;
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}

		//**************************************************************************              
		///    <Description>
		///		Create the query string to search the vendor 
		///    </Description>
		///    <Inputs>
		///     
		///     Type 0 : find to Edit by Code
		///     Type 1 : find to Edit by Name
		///     Type 2 : find to Add by Code
		///     Type 3 : find to Add by Name
		///    </Inputs>
		///    <Outputs>
		///    </Outputs>
		///    <Returns>
		///       string
		///    </Returns>
		///    <Authors>
		///       TuanDm
		///    </Authors>
		///    <History>
		///       Monday, Apr 04, 2005
		///    </History>
		///    <Notes>
		///    </Notes>
		//**************************************************************************
		//[AutoComplete(true)]
		public string ConditionToSearchVendorByFunctionCode(string pstrValue, string pstrFunctionCode, int type)
		{
			string strConditon = string.Empty;
			switch (type)
			{
				case 0:
					strConditon =	" Where Type = 1 and (Select count(*) From " + PO_FunctionVendorReferenceTable.TABLE_NAME 
						+ " A inner join " + MST_FunctionTable.TABLE_NAME + " B on A." + PO_FunctionVendorReferenceTable.FUNCTIONID_FLD + " = B." + MST_FunctionTable.FUNCTIONID_FLD
						+	" Where B.Code like '" + pstrFunctionCode.Trim() + "%' and PartyID = A.VendorID) > 0";
					break;
				case 1:
					strConditon =	" Where Type = 1 and (Select count(*) From " + PO_FunctionVendorReferenceTable.TABLE_NAME +
						" A inner join " + MST_FunctionTable.TABLE_NAME + " B on A." +  PO_FunctionVendorReferenceTable.FUNCTIONID_FLD + " = B." + MST_FunctionTable.FUNCTIONID_FLD
						+	" Where B.Code like '" + pstrFunctionCode.Trim() + "%' and PartyID = A.VendorID) > 0";
					break;
				case 2:
					strConditon = " Where Type = 1 and (Select count(*) From PO_FunctionVendorReference A Where A.VendorID = PartyID) <   (Select count(*) From MST_Function Where Code like '" + pstrFunctionCode.Trim() + "%')";
					break;
				case 3:
					strConditon = " Where Type = 1 and ( Select count(*) From PO_FunctionVendorReference A Where A.VendorID = PartyID) <   (Select count(*) From MST_Function Where Code like '" + pstrFunctionCode.Trim() + "%')";
					break;
			}
			return strConditon;
		}
		
		//**************************************************************************              
		///    <Description>
		///		Create the query string to search the function 
		///    </Description>
		///    <Inputs>
		///     Type 0 : find to Edit by VendorCode
		///     Type 1 : find to Edit by VendorName
		///     Type 2 : find to Add by VendorCode
		///     Type 3 : find to Add by VendorName
		///    </Inputs>
		///    <Outputs>
		///    </Outputs>
		///    <Returns>
		///       string
		///    </Returns>
		///    <Authors>
		///       TuanDm
		///    </Authors>
		///    <History>
		///       Monday, Apr 04, 2005
		///    </History>
		///    <Notes>
		///    </Notes>
		//**************************************************************************
		//[AutoComplete(true)]
		public string ConditionToSearchFunctionByVendor(string pstrValue, string pstrValueOfVendor, int type)
		{
			string strConditon = string.Empty;
			switch (type)
			{
				case 0:
					strConditon = " Where FunctionID in (Select FunctionID from PO_FunctionVendorReference inner join MST_Party on PO_FunctionVendorReference.VendorID = MST_Party.PartyID"
						+ " and MST_Party.Code like '%" + pstrValueOfVendor + "')";
					break;
				case 1:
					strConditon = " Where FunctionID in (Select FunctionID from PO_FunctionVendorReference inner join MST_Party on PO_FunctionVendorReference.VendorID = MST_Party.PartyID"
						+ " and MST_Party.Name like '%" + pstrValueOfVendor + "')";
					break;
				case 2:
					strConditon = " Where FunctionID not in (Select FunctionID from PO_FunctionVendorReference inner join MST_Party on PO_FunctionVendorReference.VendorID = MST_Party.PartyID"
						+ " and MST_Party.Code like '%" + pstrValueOfVendor + "')";
					break;
				case 3:
					strConditon = " Where FunctionID not in (Select FunctionID from PO_FunctionVendorReference inner join MST_Party on PO_FunctionVendorReference.VendorID = MST_Party.PartyID"
						+ " and MST_Party.Name like '%" + pstrValueOfVendor + "')";
					break;
			}
			return strConditon;
		}
	}
}
